package jp.bsws.action.syukei;

/*
 * ログインサンプル
 */
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpSession;

import jp.bsws.bean.KeyValueBean;
import jp.bsws.bean.MasterBean;
import jp.bsws.bean.NenkanJohoBean;
import jp.bsws.bl.BSWSBl;
import jp.bsws.common.BSWSConstants;
import jp.bsws.common.BSWSSession;
import jp.bsws.common.BSWSUtils;
import jp.bsws.common.DBUtils;
import jp.bsws.dao.HosyuShubetsuDao;
import jp.bsws.dao.JugyoinDao;
import jp.bsws.dao.RaitenYoinDao;
import jp.bsws.dao.ShiharaiHohoDao;
import jp.bsws.dao.UriageShubetsuDao;
import jp.bsws.model.HosyuShubetsuModel;
import jp.bsws.model.RaitenYoinModel;
import jp.bsws.model.ShiharaiHohoModel;
import jp.bsws.model.UriageShubetsuModel;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
/*
 * メインメニューアクションクラス
 */
public class  NenkanHyoAction extends ActionSupport {
	private static final long serialVersionUID = 1L;

	private BSWSSession bSWSSession;

	private String strSansyoTenpoCod;
	private Integer intSansyoJugyoinSeq;
	private String strSansyoNen;

	// ログインユーザ
	private List<KeyValueBean> kvTenpoList;
	private List<KeyValueBean> kvJugyoinList;
	private List<KeyValueBean> kvNenList;

	private List<NenkanJohoBean> nenkanHyoList;
	private List<MasterBean> raitenYoinHList;
	private List<MasterBean> uriageShubetsuHList;
	private List<MasterBean> shiharaiHohoHList;
	private List<MasterBean> hosyuShubetsuHList;
	private List<NenkanJohoBean> nenkanHyoGokeiList;
	private List<NenkanJohoBean> nenkanHyoAveList;
	private List<NenkanJohoBean> nenkanHyoYosokuList;

	// ■ログインチェック
	public String execute() throws Exception{

		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);

		Connection con = DBUtils.getConnection();
		try{

			//参照可能店舗リスト
			this.kvTenpoList = bSWSSession.getSansyoTenpoList();
			//参照店舗がnullの場合、セッションから店舗を取得
			if(BSWSUtils.isBlankNull(strSansyoTenpoCod)){
//				strSansyoTenpoCod = bSWSSession.getStrTenpoCod();
			}else{
				bSWSSession.setStrTenpoCod(strSansyoTenpoCod) ;
			}

			//参照可能従業員リスト
			this.kvJugyoinList = new ArrayList<KeyValueBean>();
			if(BSWSConstants.C_JUGYOIN_SANSYO_ALL.equals(bSWSSession.getStrJugyoinSansyoKbn())){
				JugyoinDao jugyoinDao = JugyoinDao.getInstance();
				this.kvJugyoinList =  jugyoinDao.getJugyoinList(con, bSWSSession.getStrGroupCod(), strSansyoTenpoCod );
			}else{
				this.kvJugyoinList.add(new KeyValueBean(bSWSSession.getStrJugyoinCod(),bSWSSession.getStrJugyoinKnj()));
			}

			//参照可能年月リストFROM
			this.kvNenList = BSWSUtils.getSansyoNenList();

			//ヘッダー情報取得
			RaitenYoinDao raitenYoinDao = RaitenYoinDao.getInstance();
//			UriageShubetsuDao uriageShubetsuDao = UriageShubetsuDao.getInstance();
//			ShiharaiHohoDao shiharaiHohoDao = ShiharaiHohoDao.getInstance();
//			HosyuShubetsuDao hosyuShubetsuDao = HosyuShubetsuDao.getInstance();

			//来店要因情報取得
			List<RaitenYoinModel> raitenYoinList= raitenYoinDao.getRaitenYoinJoho(con, null, bSWSSession.getStrGroupCod(), strSansyoTenpoCod);
//			//売上種別情報取得
//			List<UriageShubetsuModel> uriageShubetsuList= uriageShubetsuDao.getUriageShubetsuJoho(con, null, bSWSSession.getStrGroupCod(), strSansyoTenpoCod);
//			//支払方法情報取得
//			List<ShiharaiHohoModel> shiharaiHohoList= shiharaiHohoDao.getShiharaiHohoJoho(con, null, bSWSSession.getStrGroupCod(), strSansyoTenpoCod);
//			//報酬種別情報取得
//			List<HosyuShubetsuModel> HosyuShubetsuList= hosyuShubetsuDao.getHosyuShubetsuJoho(con, null, bSWSSession.getStrGroupCod(), strSansyoTenpoCod);

			raitenYoinHList = new ArrayList<MasterBean>();
//			uriageShubetsuHList = new ArrayList<MasterBean>();
//			shiharaiHohoHList = new ArrayList<MasterBean>();
//			hosyuShubetsuHList = new ArrayList<MasterBean>();
			// ヘッダーの取得
			for(RaitenYoinModel raitenYoinModel : raitenYoinList) {
				if(BSWSConstants.C_HYOJI.equals(raitenYoinModel.getStrRaitenYoinHyojiFlg())){
					MasterBean masterBean = new MasterBean();
					masterBean.setIntSeq(raitenYoinModel.getSeq());
					masterBean.setStrValue(raitenYoinModel.getStrRaitenYoinRyakuKnj());
					raitenYoinHList.add(masterBean);
				}
			}
//			for(UriageShubetsuModel uriageShubetsuModel : uriageShubetsuList) {
//				if(BSWSConstants.C_HYOJI.equals(uriageShubetsuModel.getStrUriageShubetsuHyojiFlg())){
//					MasterBean masterBean = new MasterBean();
//					masterBean.setIntSeq(uriageShubetsuModel.getSeq());
//					masterBean.setStrValue(uriageShubetsuModel.getStrUriageShubetsuRyakuKnj());
//					uriageShubetsuHList.add(masterBean);
//				}
//			}
//			for(ShiharaiHohoModel shiharaiHohoModel : shiharaiHohoList) {
//				if(BSWSConstants.C_HYOJI.equals(shiharaiHohoModel.getStrShiharaiHohoHyojiFlg())){
//					MasterBean masterBean = new MasterBean();
//					masterBean.setIntSeq(shiharaiHohoModel.getSeq());
//					masterBean.setStrValue(shiharaiHohoModel.getStrShiharaiHohoRyakuKnj());
//					shiharaiHohoHList.add(masterBean);
//				}
//			}
//			for(HosyuShubetsuModel hosyuShubetsuModel : HosyuShubetsuList) {
//				if(BSWSConstants.C_HYOJI.equals(hosyuShubetsuModel.getStrHosyuShubetsuHyojiFlg())){
//					MasterBean masterBean = new MasterBean();
//					masterBean.setIntSeq(hosyuShubetsuModel.getSeq());
//					masterBean.setStrValue(hosyuShubetsuModel.getStrHosyuShubetsuRyakuKnj());
//					hosyuShubetsuHList.add(masterBean);
//				}
//			}

			BSWSBl bSWSBl = new BSWSBl();

			//日付の加工
//			String tmpFromYmd = "";
//			String tmpToYmd = "";
			//どちらも空（初期表示）の場合、システム日付の年月で検索
			if(BSWSUtils.isBlankNull(strSansyoNen)){
				strSansyoNen = BSWSUtils.getSysDateYm().substring(0,4);
			}

			//集計情報取得
			nenkanHyoList = bSWSBl.getNenkanJoho(con, bSWSSession,
					raitenYoinHList,uriageShubetsuHList,shiharaiHohoHList,hosyuShubetsuHList,
					strSansyoTenpoCod, intSansyoJugyoinSeq, strSansyoNen);

			NenkanJohoBean tmpGokei = bSWSBl.getGokeiNenkanBean(nenkanHyoList, strSansyoNen);
			nenkanHyoGokeiList = new ArrayList<NenkanJohoBean>();
			nenkanHyoAveList = new ArrayList<NenkanJohoBean>();
			nenkanHyoYosokuList = new ArrayList<NenkanJohoBean>();
			//合計値取得
			nenkanHyoGokeiList.add(tmpGokei);
			//平均値取得（画面で計算）
			nenkanHyoAveList.add(tmpGokei);
			//予測値取得（画面で計算）
			nenkanHyoYosokuList.add(tmpGokei);

		}catch(SQLException e){
			con.rollback();
			throw e;
		}finally{
			con.close();
		}
		return SUCCESS;
	}

	public BSWSSession getBSWSSession() {
		return bSWSSession;
	}

	public void setBSWSSession(BSWSSession bSWSSession) {
		this.bSWSSession = bSWSSession;
	}

	public String getStrSansyoTenpoCod() {
		return strSansyoTenpoCod;
	}

	public void setStrSansyoTenpoCod(String strSansyoTenpoCod) {
		this.strSansyoTenpoCod = strSansyoTenpoCod;
	}

	public Integer getIntSansyoJugyoinSeq() {
		return intSansyoJugyoinSeq;
	}

	public void setIntSansyoJugyoinSeq(Integer intSansyoJugyoinSeq) {
		this.intSansyoJugyoinSeq = intSansyoJugyoinSeq;
	}

	public List<KeyValueBean> getKvTenpoList() {
		return kvTenpoList;
	}

	public void setKvTenpoList(List<KeyValueBean> kvTenpoList) {
		this.kvTenpoList = kvTenpoList;
	}

	public List<KeyValueBean> getKvJugyoinList() {
		return kvJugyoinList;
	}

	public void setKvJugyoinList(List<KeyValueBean> kvJugyoinList) {
		this.kvJugyoinList = kvJugyoinList;
	}

	public List<NenkanJohoBean> getNenkanHyoList() {
		return nenkanHyoList;
	}

	public void setNenkanHyoList(List<NenkanJohoBean> nenkanHyoList) {
		this.nenkanHyoList = nenkanHyoList;
	}

	public List<MasterBean> getRaitenYoinHList() {
		return raitenYoinHList;
	}

	public void setRaitenYoinHList(List<MasterBean> raitenYoinHList) {
		this.raitenYoinHList = raitenYoinHList;
	}

	public List<MasterBean> getUriageShubetsuHList() {
		return uriageShubetsuHList;
	}

	public void setUriageShubetsuHList(List<MasterBean> uriageShubetsuHList) {
		this.uriageShubetsuHList = uriageShubetsuHList;
	}

	public List<MasterBean> getShiharaiHohoHList() {
		return shiharaiHohoHList;
	}

	public void setShiharaiHohoHList(List<MasterBean> shiharaiHohoHList) {
		this.shiharaiHohoHList = shiharaiHohoHList;
	}

	public List<MasterBean> getHosyuShubetsuHList() {
		return hosyuShubetsuHList;
	}

	public void setHosyuShubetsuHList(List<MasterBean> hosyuShubetsuHList) {
		this.hosyuShubetsuHList = hosyuShubetsuHList;
	}

	public List<NenkanJohoBean> getNenkanHyoGokeiList() {
		return nenkanHyoGokeiList;
	}

	public void setNenkanHyoGokeiList(List<NenkanJohoBean> nenkanHyoGokeiList) {
		this.nenkanHyoGokeiList = nenkanHyoGokeiList;
	}

	public List<NenkanJohoBean> getNenkanHyoAveList() {
		return nenkanHyoAveList;
	}

	public void setNenkanHyoAveList(List<NenkanJohoBean> nenkanHyoAveList) {
		this.nenkanHyoAveList = nenkanHyoAveList;
	}

	public List<NenkanJohoBean> getNenkanHyoYosokuList() {
		return nenkanHyoYosokuList;
	}

	public void setNenkanHyoYosokuList(List<NenkanJohoBean> nenkanHyoYosokuList) {
		this.nenkanHyoYosokuList = nenkanHyoYosokuList;
	}

	public String getStrSansyoNen() {
		return strSansyoNen;
	}

	public void setStrSansyoNen(String strSansyoNen) {
		this.strSansyoNen = strSansyoNen;
	}

	public List<KeyValueBean> getKvNenList() {
		return kvNenList;
	}

	public void setKvNenList(List<KeyValueBean> kvNenList) {
		this.kvNenList = kvNenList;
	}
}